/**
 * @param {number[]} nums
 */
var NumArray = function (nums) {
    this.nums = nums;
};

/** 
 * @param {number} left 
 * @param {number} right
 * @return {number}
 */
NumArray.prototype.sumRange = function (left, right) {
    let sum = 0;
    for (let i = left; i <= right; i++) {
        sum += this.nums[i];
    }
    return sum;
};


// 方法2：由于调用sumRange的次数多，因此在最开始就先加好存储在数组中，更加节省时间。
var NumArray = function (nums) {
    const len = nums.length;
    this.nums = new Array(len + 1).fill(0);
    for (let i = 0; i < len; i++) {
        this.nums[i + 1] += this.nums[i] + nums[i];
    }
};

/** 
 * @param {number} left 
 * @param {number} right
 * @return {number}
 */
NumArray.prototype.sumRange = function (left, right) {
    return this.nums[right + 1] - this.nums[left];
};
